/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    usart.c
  * @brief   This file provides code for the configuration
  *          of the USART instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usart.h"

/* USER CODE BEGIN 0 */
#include "motor.h"
#include "stdio.h"
#include "string.h"
#include "FreeRTOS.h"
#include "cmsis_os.h"


Transmit_Buf_Struct Transmit_Buf[12];
Receive_Buf_Struct Receive_Buf;
uint8_t RxBuffer;
uint8_t RxBuffer_List[RX_MAXLEN];

/* USER CODE END 0 */

UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;

/* USART1 init function */

void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */
  // HAL_UART_Receive_IT(&huart1, &RxBuffer, 1); // ���������ж�

  /* USER CODE END USART1_Init 2 */

}
/* USART2 init function */

void MX_USART2_UART_Init(void)
{

  /* USER CODE BEGIN USART2_Init 0 */

  /* USER CODE END USART2_Init 0 */

  /* USER CODE BEGIN USART2_Init 1 */

  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */
	HAL_UART_Receive_IT(&huart2, &RxBuffer, 1);
  /* USER CODE END USART2_Init 2 */

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */

  /* USER CODE END USART1_MspInit 0 */
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspInit 1 */

  /* USER CODE END USART1_MspInit 1 */
  }
  else if(uartHandle->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspInit 0 */

  /* USER CODE END USART2_MspInit 0 */
    /* USART2 clock enable */
    __HAL_RCC_USART2_CLK_ENABLE();

    __HAL_RCC_GPIOD_CLK_ENABLE();
    /**USART2 GPIO Configuration
    PD5     ------> USART2_TX
    PD6     ------> USART2_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

    /* USART2 interrupt Init */
    HAL_NVIC_SetPriority(USART2_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
  /* USER CODE BEGIN USART2_MspInit 1 */
	
  /* USER CODE END USART2_MspInit 1 */
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspDeInit 0 */

  /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();

    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

    /* USART1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspDeInit 1 */

  /* USER CODE END USART1_MspDeInit 1 */
  }
  else if(uartHandle->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspDeInit 0 */

  /* USER CODE END USART2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART2_CLK_DISABLE();

    /**USART2 GPIO Configuration
    PD5     ------> USART2_TX
    PD6     ------> USART2_RX
    */
    HAL_GPIO_DeInit(GPIOD, GPIO_PIN_5|GPIO_PIN_6);

    /* USART2 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART2_IRQn);
  /* USER CODE BEGIN USART2_MspDeInit 1 */

  /* USER CODE END USART2_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */

// 发送数据函数
HAL_StatusTypeDef Uart_Transmit(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
	HAL_StatusTypeDef status;
	status = HAL_UART_Transmit(huart, pData, Size, Timeout);
	return status;
}

void Transmit_Buf_Struct_Get(Transmit_Buf_Struct* Transmit_Buffer)
{
//	// Speed
//	// X Speed
//	strcpy(Transmit_Buffer[0].para_id_name,"xs");
//	sprintf(Transmit_Buffer[0].para_string, " %0.3f" , vx_meas);
//	// X Speed ref
//	strcpy(Transmit_Buffer[1].para_id_name,"xsr");
//	sprintf(Transmit_Buffer[1].para_string, " %0.3f" , (double)ADJUST_MAX_SPEED_XY);
//	// Y Speed
//	strcpy(Transmit_Buffer[2].para_id_name,"ys");
//	sprintf(Transmit_Buffer[2].para_string, " %0.3f" , vy_meas);
//	// Y Speed ref
//	strcpy(Transmit_Buffer[3].para_id_name,"ysr");
//	sprintf(Transmit_Buffer[3].para_string, " %0.3f" , (double)ADJUST_MAX_SPEED_XY);
//	// Z Speed
//	strcpy(Transmit_Buffer[4].para_id_name,"zs");
//	sprintf(Transmit_Buffer[4].para_string, " %0.3f" , vz_meas);
//	// Z Speed ref
//	strcpy(Transmit_Buffer[5].para_id_name,"zsr");
//	sprintf(Transmit_Buffer[5].para_string, " %0.3f" , (double)ADJUST_MAX_SPEED_Z);
	
//	// position
//	// X position
//	strcpy(Transmit_Buffer[6].para_id_name,"xp");
//	sprintf(Transmit_Buffer[6].para_string, " %0.3f" , odometry_x);
//	// X position ref
//	strcpy(Transmit_Buffer[7].para_id_name,"xpr");
//	sprintf(Transmit_Buffer[7].para_string, " %0.3f" , dest_x);
//	// Y position
//	strcpy(Transmit_Buffer[8].para_id_name,"yp");
//	sprintf(Transmit_Buffer[8].para_string, " %0.3f" , odometry_y);
//	// Y position ref
//	strcpy(Transmit_Buffer[9].para_id_name,"ypr");
//	sprintf(Transmit_Buffer[9].para_string, " %0.3f" , dest_y);
//	// Z position
//	strcpy(Transmit_Buffer[10].para_id_name,"zp");
//	sprintf(Transmit_Buffer[10].para_string, " %0.3f" , odometry_z);
//	// Z position ref
//	strcpy(Transmit_Buffer[11].para_id_name,"zpr");
//	sprintf(Transmit_Buffer[11].para_string, " %0.3f" , dest_z);
//	
	// Speed
	// X Speed
	strcpy(Transmit_Buffer[0].para_id_name,"xs");
	Transmit_Buffer[0].para_string = vx_meas;
	// X Speed ref
	strcpy(Transmit_Buffer[1].para_id_name,"xsr");
	Transmit_Buffer[1].para_string = (double)ADJUST_MAX_SPEED_XY;
	// Y Speed
	strcpy(Transmit_Buffer[2].para_id_name,"ys");
	Transmit_Buffer[2].para_string = vy_meas;
	// Y Speed ref
	strcpy(Transmit_Buffer[3].para_id_name,"ysr");
	Transmit_Buffer[3].para_string = (double)ADJUST_MAX_SPEED_XY;
	// Z Speed
	strcpy(Transmit_Buffer[4].para_id_name,"zs");
	Transmit_Buffer[4].para_string = vz_meas;
	// Z Speed ref
	strcpy(Transmit_Buffer[5].para_id_name,"zsr");
	Transmit_Buffer[5].para_string = (double)ADJUST_MAX_SPEED_Z;
	
	// position
	// X position
	strcpy(Transmit_Buffer[6].para_id_name,"xp");
	Transmit_Buffer[6].para_string = odometry_x;
	// X position ref
	strcpy(Transmit_Buffer[7].para_id_name,"xpr");
	Transmit_Buffer[7].para_string = dest_x;
	// Y position
	strcpy(Transmit_Buffer[8].para_id_name,"yp");
	Transmit_Buffer[8].para_string = odometry_y;
	// Y position ref
	strcpy(Transmit_Buffer[9].para_id_name,"ypr");
	Transmit_Buffer[9].para_string = dest_y;
	// Z position
	strcpy(Transmit_Buffer[10].para_id_name,"zp");
	Transmit_Buffer[10].para_string = odometry_z;
	// Z position ref
	strcpy(Transmit_Buffer[11].para_id_name,"zpr");
	Transmit_Buffer[11].para_string = dest_z;
}










//// ��д���ճɹ����жϷ�����
//void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
//{
//	if(huart == &huart1)
//	{
//		// ���ճɹ��������յ����ݷŵ����������У����д�����л��Ѷ�Ӧ����?
//		// ��������д����е����?
//		osStatus status = osMessagePut(Uart1CmdHandle, (uint32_t)RxBuffer, 0); // ���Ѷ��ö��е���������������
//		if(status != osOK)
//		{
//			printf("Error\n");
//		}
//		else
//		{
//			// �����ջ�����ַ�ת������ŵ�Receive_Buf
//			sscanf((char*)RxBuffer,"%hhu %f %f %f %f %f %f", &Receive_Buf.Receive_Cmd, \
//			&Receive_Buf.Receive_Para[0], &Receive_Buf.Receive_Para[1], &Receive_Buf.Receive_Para[2], \
//			&Receive_Buf.Receive_Para[3], &Receive_Buf.Receive_Para[4], &Receive_Buf.Receive_Para[5]);
//			HAL_UART_Receive_IT(&huart1, RxBuffer, RX_MAXLEN); // ���¿��������ж�
//		}
//		
//	}
//}

/* USER CODE END 1 */
